package com.xxl.controller;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

/**
 * 全局控制类
 * @author xxl
 * @date 2023/3/15
 */
@ControllerAdvice
public class IncorrectCredentialsExceptionController {
    /**
     *  全局捕获异常，首先不能有try-catch捕获了
     */
    @ExceptionHandler(IncorrectCredentialsException.class)
    public String error() {
        System.out.println("捕获到IncorrectCredentialsException异常=密码错误");
        return "/index";
    }

    /**
     * 全局传递Model
     */
    @ModelAttribute
    public void ModelAttribute1(Model model) {
        model.addAttribute("ModelAttribute1","全局可以调用的ModelAttribute1，只要是使用了Model类");
    }
    /**
     * 全局了的controller校验
     */
    @InitBinder
    private void initBinder(WebDataBinder binder) {
        System.out.println(binder.getFieldDefaultPrefix());
        System.out.println("被@InitBinder注解的方法会在请求到达之前使用此方法，跟@Before差不多");
    }
}
